package com.tangding.rabbitmq.client;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

import org.junit.Before;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.tangding.common.utils.Utils;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月9日_下午4:38:27
 *
 */
public class RabbitMQBaseClient {
	ConnectionFactory factory = new ConnectionFactory();
    
    //创建一个新的连接
	Connection connection = null;

    @Before
	public void init() throws IOException, TimeoutException{
		factory.setHost("127.0.0.1");
		factory.setUsername("moon");
		factory.setPassword("summer");
		factory.setVirtualHost("/dadong");
		
		ExecutorService service = Executors.newFixedThreadPool(10);  
        factory.setSharedExecutor(service);
		
		factory.setAutomaticRecoveryEnabled(true);
		factory.setNetworkRecoveryInterval(5000L);
		factory.setTopologyRecoveryEnabled(false);
	}
    
    private Map<String, Channel> channels = new HashMap<>();
    
    private Channel buildOrBindExchange(String key) throws IOException, TimeoutException {
    	if (channels.containsKey(key)) {
			return channels.get(key);
		}
    	
    	//创建一个新的连接
		connection = factory.newConnection();
		
    	//创建一个通道
        Channel channel = connection.createChannel();
        try {
			channel.exchangeBind(key, key + Utils.randomString(5), key + Utils.randomString(5));
		} catch (Exception e) {
			channel = connection.createChannel();
			//定义一个交换机
			//参数1：交换机名称
			//参数2：交换机类型
			//参数3：交换机持久性，如果为true则服务器重启时不会丢失
			//参数4：交换机在不被使用时是否删除
			//参数5：交换机的其他属性
			channel.exchangeDeclare(key, "fanout", false, true, null);
		}
        channels.put(key, channel);
        return channel;
    }
    
    
    private Channel buildOrBindExchange(String key, String routingkey) throws IOException, TimeoutException {
    	if (channels.containsKey(key)) {
			return channels.get(key);
		}
    	
    	//创建一个新的连接
		connection = factory.newConnection();
		
    	//创建一个通道
        Channel channel = connection.createChannel();
        try {
			channel.exchangeBind(key, key, routingkey);
		} catch (Exception e) {
			channel = connection.createChannel();
			//定义一个交换机
			//参数1：交换机名称
			//参数2：交换机类型
			//参数3：交换机持久性，如果为true则服务器重启时不会丢失
			//参数4：交换机在不被使用时是否删除
			//参数5：交换机的其他属性
			channel.exchangeDeclare(key, "direct", false, true, null);
		}
        channels.put(key, channel);
        return channel;
    }

    protected void send(String key, String msg) {
		try {
			buildOrBindExchange(key).basicPublish(key, "", null, msg.getBytes("UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
	}
    
    protected void send(String key, String routingKey, String msg) {
		try {
			buildOrBindExchange(key, routingKey).basicPublish(key, routingKey, null, msg.getBytes("UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
	}
    
    protected Map<String, Object> buildPushData(String cmd, Object data) {
		Map<String, Object> push = new HashMap<>();
		push.put("cmd", cmd);
		push.put("data", data);
		return push;
	}
}
